package com.pt25.base.util;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

public class SystemInfoUtil {

    /**
     * 获取逻辑处理器核心数
     */
    public static int getLogicalProcessorCount() {
        return Runtime.getRuntime().availableProcessors();
    }

    /**
     * 获取系统详细信息
     */
    public static void printSystemInfo() {
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();

        System.out.println("=== 系统信息 ===");
        System.out.println("操作系统: " + osBean.getName() + " " + osBean.getVersion());
        System.out.println("系统架构: " + osBean.getArch());
        System.out.println("逻辑处理器数: " + getLogicalProcessorCount());
        System.out.println("系统负载(最近1分钟): " + osBean.getSystemLoadAverage());

        // 内存信息
        System.out.println("最大内存: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB");
        System.out.println("总内存: " + Runtime.getRuntime().totalMemory() / 1024 / 1024 + "MB");
        System.out.println("空闲内存: " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "MB");
    }

    /**
     * 获取推荐线程池大小
     */
    public static int getRecommendedThreadPoolSize() {
        int cores = getLogicalProcessorCount();
        // IO密集型任务：2 * 核心数
        // CPU密集型任务：核心数 + 1
        return cores + 1;
    }

    /**
     * 获取IO密集型任务线程池大小
     */
    public static int getIOIntensiveThreadPoolSize() {
        return getLogicalProcessorCount() * 2;
    }

}
